package base.tag;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;

public class BsGridTag extends BsContainerTag<BsColumnModel> {

	private static final long serialVersionUID = 1L;
	private String[] fhead = null;
	
	private String headDepth = "1";
	private String dataUrl = "";
	private String paging = "false";
	private String pageSize = "20";
	private String editable = "false";
	private String queryParams = "";
	private String mutilSelect = "false";
	
	public BsGridTag() {
		items = new ArrayList<BsColumnModel>();
	}
	
	@Override
	public int doStartTag() throws JspException {
		
		items.clear();
		return Tag.EVAL_PAGE;
	}	
	
	@Override
	public int doEndTag() throws JspException {
		
		String fheight = height;
		if(this.usedHeight.compareTo("") != 0)
		{
			fheight = "$(document).height() - " + this.usedHeight;
		}

		fhead = new String[Integer.parseInt(headDepth)];
		for(int i = 0; i < fhead.length; i++)
			fhead[i] = "";
		if(mutilSelect.compareTo("true") == 0)
			fhead[0] = "<th colspan=1 rowspan=" + headDepth + " data-checkbox=true></th>\n";
		else
			fhead[0] = "<th colspan=1 rowspan=" + headDepth + " data-radio=true></th>\n";
			

		String fattr = " data-show-columns=true data-search=true data-click-to-select=true data-show-toggle=true data-show-refresh=true ";
		String fqueryParam = "";
		if(queryParams.compareTo("") != 0)
		{
			String[] tparams = queryParams.split(",");
			for(int i = 0; i < tparams.length; i++)
			{
				fqueryParam += tparams[i] + ":$('#" + tparams[i] + "').val()";
				if(i < tparams.length - 1)
					fqueryParam += ",";
			}
		}

		StringBuffer tag = new StringBuffer();
		tag.append("<div class=\"col-xs-" + colWidth + "\">\n");
		tag.append("<div class=\"panel panel-info\">\n");
		if (caption.compareTo("") != 0)
		{
			tag.append("<div class=\"panel-heading\">");
			tag.append("<h3 class=\"panel-title\">" + caption + "</h3>");
			tag.append("</div>\n");
		}	
        if(paging.compareTo("true") == 0)
        {
        	fattr += " data-search-on-enter-key=true data-pagination=true data-silent-sort=true data-side-pagination='server' data-page-number=1 data-page-size=" + pageSize + " ";
        	if(fqueryParam.compareTo("") != 0)
        	  fqueryParam += ",limit:params.limit,offset: params.offset,order: params.order,sort: params.sort,search:params.search";
        	else
          	  fqueryParam += "limit:params.limit,offset: params.offset,order: params.order,sort: params.sort,search:params.search";
        }  
        if(toolbar.compareTo("") != 0)
        	fattr += " data-toolbar='#" + toolbar + "' ";
        tag.append("<table id='" + name + "' data-url='" + dataUrl  + "'" + fattr + ">\n");
		tag.append("<thead>\n");
		setHead(items);
		for (int i = 0; i < fhead.length; i++)
			tag.append("<tr>" + fhead[i] + "</tr>\n");
		tag.append("</thead>\n");
		tag.append("</table>\n");
		tag.append("</div>\n");
		tag.append("</div>\n");
		//script
		tag.append("<script type=\"text/javascript\">\n");
		if(fqueryParam.compareTo("") != 0)
		{
			tag.append("function " + name + "paramfunc(params)\n {var tparam = {" + fqueryParam + "};return tparam;} \n");
			tag.append("bindGridData('" + name + "', " + name + "paramfunc);\n");
		}else
			tag.append("bindGridData('" + name + "')\n");
		if(fheight.compareTo("") != 0)
			tag.append("$('#" + name + "').bootstrapTable('resetView',{height:" + fheight + "});\n");
		tag.append("</script>\n");
		output(tag);
		
		return Tag.EVAL_PAGE;
	}
	
	private void setHead(List<BsColumnModel> list)
	{
		if (list.size() == 0)
			return;
		int rows = 0;
		int cols = 0;
		int hcount = Integer.parseInt(headDepth);
		int index = 0;
		for (int i = 0; i < list.size() ; i++)
		{
			if (list.get(i).getItems().size() > 0)
			{
				rows = 1;
				cols = list.get(i).getColspan();
			}
			else{
				rows = hcount - (list.get(i).getLevel() - 1);
				cols = 1;
			}
			index = hcount - (hcount - list.get(i).getLevel()) - 1;
			String sfield = "";
			if(list.get(i).getField().compareTo("") != 0)
				sfield += " data-field='" + list.get(i).getField() + "' data-editable=" + editable + " data-sortable=true ";
			if(list.get(i).getWidth().compareTo("") != 0 && list.get(i).getField().compareTo("") != 0)
				sfield += " data-width=" + list.get(i).getWidth() + " ";
			fhead[index] += "<th colspan='" + cols + "' rowspan='" + rows  + "' " + sfield
						 +  " data-valign='middle' data-align='center'>"  + list.get(i).getCaption()
						 +  "</th>\n";
			
			if (list.get(i).getItems().size() > 0)
			  setHead(list.get(i).getItems());
		}
		
	}


	public String getHeadDepth() {
		return headDepth;
	}

	public void setHeadDepth(String headDepth) {
		this.headDepth = headDepth;
	}

	public String getDataUrl() {
		return dataUrl;
	}

	public void setDataUrl(String dataUrl) {
		this.dataUrl = dataUrl;
	}

	public String getPaging() {
		return paging;
	}

	public void setPaging(String paging) {
		this.paging = paging;
	}

	public String getPageSize() {
		return pageSize;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public String getEditable() {
		return editable;
	}

	public void setEditable(String editable) {
		this.editable = editable;
	}

	public String getQueryParams() {
		return queryParams;
	}

	public void setQueryParams(String queryParams) {
		this.queryParams = queryParams;
	}

	public String getMutilSelect() {
		return mutilSelect;
	}

	public void setMutilSelect(String mutilSelect) {
		this.mutilSelect = mutilSelect;
	}

		
}
